package com.zenithmind.chat.controller;

import com.zenithmind.chat.pojo.entity.ChatGroupMember;
import com.zenithmind.chat.pojo.vo.ChatUserVO;
import com.zenithmind.chat.service.ChatGroupMemberService;
import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 群组成员管理控制器
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Tag(name = "群组成员管理", description = "群组成员相关接口")
@Slf4j
@RestController
@RequestMapping("/api/zenithMind/chat/group-member")
@RequiredArgsConstructor
public class ChatGroupMemberController {

    private final ChatGroupMemberService groupMemberService;

    @Operation(summary = "分页查询群组成员列表")
    @GetMapping("/page")
    public Result<PageResult<ChatUserVO>> getGroupMemberPage(
            @Parameter(description = "群组ID") @RequestParam String groupId,
            @Parameter(description = "成员状态") @RequestParam(required = false) Integer status,
            @Parameter(description = "成员角色") @RequestParam(required = false) Integer role,
            @Parameter(description = "搜索关键词") @RequestParam(required = false) String keyword,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "页大小") @RequestParam(defaultValue = "10") Integer pageSize) {
        
        PageResult<ChatUserVO> result = groupMemberService.getGroupMemberPage(
            groupId, status, role, keyword, pageNum, pageSize);
        
        return Result.success(result);
    }

    @Operation(summary = "查询群组成员列表")
    @GetMapping("/list")
    public Result<List<ChatUserVO>> getGroupMembers(
            @Parameter(description = "群组ID") @RequestParam String groupId,
            @Parameter(description = "成员状态") @RequestParam(required = false) Integer status) {
        
        List<ChatUserVO> result = groupMemberService.getGroupMembers(groupId, status);
        
        return Result.success(result);
    }

    @Operation(summary = "查询群组管理员列表")
    @GetMapping("/admins")
    public Result<List<ChatUserVO>> getGroupAdmins(
            @Parameter(description = "群组ID") @RequestParam String groupId) {
        
        List<ChatUserVO> result = groupMemberService.getGroupAdmins(groupId);
        
        return Result.success(result);
    }

    @Operation(summary = "查询用户在群组中的信息")
    @GetMapping("/info")
    public Result<ChatUserVO> getGroupMemberInfo(
            @Parameter(description = "群组ID") @RequestParam String groupId,
            @Parameter(description = "用户ID") @RequestParam(required = false) String userId) {
        
        String targetUserId = userId != null ? userId : UserContext.getUserId();
        ChatUserVO result = groupMemberService.getGroupMemberInfo(groupId, targetUserId);
        
        return Result.success(result);
    }

    @Operation(summary = "添加群组成员")
    @PostMapping("/add")
    public Result<Boolean> addGroupMember(
            @Parameter(description = "群组ID") @RequestParam String groupId,
            @Parameter(description = "用户ID") @RequestParam String userId,
            @Parameter(description = "成员角色") @RequestParam(required = false) Integer role) {
        
        String operatorId = UserContext.getUserId();
        Boolean result = groupMemberService.addGroupMember(groupId, userId, role, operatorId);
        
        return Result.success(result);
    }

    @Operation(summary = "批量添加群组成员")
    @PostMapping("/batch-add")
    public Result<Boolean> batchAddGroupMembers(
            @Parameter(description = "群组ID") @RequestParam String groupId,
            @Parameter(description = "用户ID列表") @RequestBody List<String> userIds) {
        
        String operatorId = UserContext.getUserId();
        Boolean result = groupMemberService.batchAddGroupMembers(groupId, userIds, operatorId);
        
        return Result.success(result);
    }

    @Operation(summary = "移除群组成员")
    @PostMapping("/remove")
    public Result<Boolean> removeGroupMember(
            @Parameter(description = "群组ID") @RequestParam String groupId,
            @Parameter(description = "用户ID") @RequestParam String userId) {
        
        String operatorId = UserContext.getUserId();
        Boolean result = groupMemberService.removeGroupMember(groupId, userId, operatorId);
        
        return Result.success(result);
    }

    @Operation(summary = "批量移除群组成员")
    @PostMapping("/batch-remove")
    public Result<Boolean> batchRemoveGroupMembers(
            @Parameter(description = "群组ID") @RequestParam String groupId,
            @Parameter(description = "用户ID列表") @RequestBody List<String> userIds) {
        
        String operatorId = UserContext.getUserId();
        Boolean result = groupMemberService.batchRemoveGroupMembers(groupId, userIds, operatorId);
        
        return Result.success(result);
    }

    @Operation(summary = "设置成员角色")
    @PostMapping("/set-role")
    public Result<Boolean> setMemberRole(
            @Parameter(description = "群组ID") @RequestParam String groupId,
            @Parameter(description = "用户ID") @RequestParam String userId,
            @Parameter(description = "新角色") @RequestParam Integer role) {
        
        String operatorId = UserContext.getUserId();
        Boolean result = groupMemberService.setMemberRole(groupId, userId, role, operatorId);
        
        return Result.success(result);
    }

    @Operation(summary = "批量设置成员角色")
    @PostMapping("/batch-set-role")
    public Result<Boolean> batchSetMemberRole(
            @Parameter(description = "群组ID") @RequestParam String groupId,
            @Parameter(description = "用户ID列表") @RequestBody List<String> userIds,
            @Parameter(description = "新角色") @RequestParam Integer role) {
        
        String operatorId = UserContext.getUserId();
        Boolean result = groupMemberService.batchSetMemberRole(groupId, userIds, role, operatorId);
        
        return Result.success(result);
    }

    @Operation(summary = "禁言成员")
    @PostMapping("/mute")
    public Result<Boolean> muteMember(
            @Parameter(description = "群组ID") @RequestParam String groupId,
            @Parameter(description = "用户ID") @RequestParam String userId,
            @Parameter(description = "禁言分钟数") @RequestParam Integer muteMinutes) {
        
        String operatorId = UserContext.getUserId();
        Boolean result = groupMemberService.muteMember(groupId, userId, muteMinutes, operatorId);
        
        return Result.success(result);
    }

    @Operation(summary = "解除禁言")
    @PostMapping("/unmute")
    public Result<Boolean> unmuteMember(
            @Parameter(description = "群组ID") @RequestParam String groupId,
            @Parameter(description = "用户ID") @RequestParam String userId) {
        
        String operatorId = UserContext.getUserId();
        Boolean result = groupMemberService.unmuteMember(groupId, userId, operatorId);
        
        return Result.success(result);
    }

    @Operation(summary = "批量禁言成员")
    @PostMapping("/batch-mute")
    public Result<Boolean> batchMuteMembers(
            @Parameter(description = "群组ID") @RequestParam String groupId,
            @Parameter(description = "用户ID列表") @RequestBody List<String> userIds,
            @Parameter(description = "禁言分钟数") @RequestParam Integer muteMinutes) {
        
        String operatorId = UserContext.getUserId();
        Boolean result = groupMemberService.batchMuteMembers(groupId, userIds, muteMinutes, operatorId);
        
        return Result.success(result);
    }

    @Operation(summary = "转让群主")
    @PostMapping("/transfer-ownership")
    public Result<Boolean> transferOwnership(
            @Parameter(description = "群组ID") @RequestParam String groupId,
            @Parameter(description = "新群主ID") @RequestParam String newOwnerId) {
        
        String currentOwnerId = UserContext.getUserId();
        Boolean result = groupMemberService.transferOwnership(groupId, newOwnerId, currentOwnerId);
        
        return Result.success(result);
    }

    @Operation(summary = "检查用户是否为群组成员")
    @GetMapping("/check-member")
    public Result<Boolean> checkIsMember(
            @Parameter(description = "群组ID") @RequestParam String groupId,
            @Parameter(description = "用户ID") @RequestParam(required = false) String userId) {
        
        String targetUserId = userId != null ? userId : UserContext.getUserId();
        Boolean result = groupMemberService.checkIsMember(groupId, targetUserId);
        
        return Result.success(result);
    }

    @Operation(summary = "检查用户是否为群组管理员")
    @GetMapping("/check-admin")
    public Result<Boolean> checkIsAdmin(
            @Parameter(description = "群组ID") @RequestParam String groupId,
            @Parameter(description = "用户ID") @RequestParam(required = false) String userId) {
        
        String targetUserId = userId != null ? userId : UserContext.getUserId();
        Boolean result = groupMemberService.checkIsAdmin(groupId, targetUserId);
        
        return Result.success(result);
    }

    @Operation(summary = "统计群组成员数量")
    @GetMapping("/count")
    public Result<Integer> countGroupMembers(
            @Parameter(description = "群组ID") @RequestParam String groupId,
            @Parameter(description = "成员状态") @RequestParam(required = false) Integer status) {
        
        Integer result = groupMemberService.countGroupMembers(groupId, status);
        
        return Result.success(result);
    }

    @Operation(summary = "更新成员最后活跃时间")
    @PostMapping("/update-active-time")
    public Result<Boolean> updateLastActiveTime(
            @Parameter(description = "群组ID") @RequestParam String groupId) {
        
        String userId = UserContext.getUserId();
        Boolean result = groupMemberService.updateLastActiveTime(groupId, userId);
        
        return Result.success(result);
    }

    @Operation(summary = "查询即将过期的禁言成员")
    @GetMapping("/expired-muted")
    public Result<List<ChatGroupMember>> getExpiredMutedMembers() {
        List<ChatGroupMember> result = groupMemberService.getExpiredMutedMembers();
        
        return Result.success(result);
    }

    @Operation(summary = "自动解除过期禁言")
    @PostMapping("/auto-unmute")
    public Result<Integer> autoUnmuteExpiredMembers() {
        Integer result = groupMemberService.autoUnmuteExpiredMembers();
        
        return Result.success(result);
    }
}
